//
//  WGDeseaseController.m
//  QuickCure1
//
//  Created by Apple on 16/11/2.
//  Copyright © 2016年 maoge. All rights reserved.
//

#import "WGDeseaseController.h"
#import "HMHomeFunctionModel.h"
#import "WGDetailDiseasesController.h"
#import "WGDiseaseFooterView.h"
#import "BJXieHeViewController.h"
#import "WGComplicationwController.h"
#import "WGDiseaseAleartView.h"
#import "YJDoctorsIfonModel.h"
#import "NSString+Path.h"
#import "YJDoctorDetailInfoController.h"
#import "WGApplyModel.h"

#define kAppplyModelFile @"applymodel.plist"

@interface WGDeseaseController ()
/**疾病名称*/
@property (weak, nonatomic) IBOutlet UILabel *lb_diseaseName;

/**疾病种类*/
@property (weak, nonatomic) IBOutlet UILabel *lb_detailDiseases;

/**疾病种类个数*/
@property (weak, nonatomic) IBOutlet UILabel *lb_detailCount;

/**是否确诊*/
@property (weak, nonatomic) IBOutlet UILabel *lb_IsQueZhen;

/**确诊选项*/
@property (weak, nonatomic) IBOutlet UIButton *btn_queZhen;

/**接受按钮*/
@property (weak, nonatomic) IBOutlet UILabel *lb_isJieShou;

/**疑似选项*/
@property (weak, nonatomic) IBOutlet UIButton *btn_yiSi;

/**已接受选项*/
@property (weak, nonatomic) IBOutlet UIButton *btn_jieShou;

/**不接受选项*/
@property (weak, nonatomic) IBOutlet UIButton *btn_noJIeShou;

/**治疗类型*/
@property (weak, nonatomic) IBOutlet UILabel *treatType;

/**就诊按钮*/
@property(nonatomic,weak)UIButton *btn_apply;

/**表尾部视图*/
@property(nonatomic,weak)WGDiseaseFooterView *footerView ;

/**弹框视图*/

@property(nonatomic,weak)WGDiseaseAleartView *aleartView;

/**医生数量*/
@property(nonatomic,assign)int doctorCount;

/**保存申请的数据*/
@property(nonatomic,strong)NSMutableArray  *applyArray;

/**遮罩*/
@property(nonatomic,weak)UIView *coverView;

/**申请成功提示框*/
@property(nonatomic,weak)UIButton *applySecButton;

/**保存组头*/

@property(nonatomic,copy)NSString *sectionHeader;


/**保存治疗类型按钮*/

@property(nonatomic,weak)UIButton *treatTypeButton;

@end

@implementation WGDeseaseController



- (void)viewDidLoad {
    [super viewDidLoad];
    
    //搭建界面
    [self setupUI];
    
    
#pragma mark - 接档数据
    //程序启动的时候进行解档
    
    NSString *filePath = [kAppplyModelFile jq_appendDocumentDir];
    
    WGApplyModel *applyModel = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    
    if (!applyModel) {
        
        //根据解档的数据赋值
        self.lb_diseaseName.text = applyModel.detailDisease;
        self.lb_detailDiseases.text = applyModel.complicationDisease;
        self.lb_detailCount.text = applyModel.complicationCount;
        self.lb_IsQueZhen.text = applyModel.quezhen;
        self.lb_isJieShou.text = applyModel.jieShou;
        self.doctorCount = applyModel.docotorCount;
        self.treatType.hidden = NO;
        
        [self.footerView.btn_apply setTitle:@"重新申请" forState:UIWindowLevelNormal];
        
        [self.footerView.btn_apply setBackgroundColor:[UIColor jq_colorWithHex:kThemeTextColor]];
        
        //调用点击按钮
        
        if ([applyModel.quezhen isEqualToString:@"已确诊"]) {
            
            [self clickQueZhenButton:self.btn_queZhen];
        }
        if([applyModel.quezhen isEqualToString:@"症状疑似"]){
            
            [self clickSimilarDiseaseButton:self.btn_yiSi];
            
        }
        if([applyModel.jieShou isEqualToString:@"接受过"]){
            [self clickReceiveButton:self.btn_jieShou];
            
        }
        if([applyModel.jieShou isEqualToString:@"未接受过"]){
            
            [self clickRefuseButton:self.btn_noJIeShou];
        }
        
    }
    
}

#pragma
#pragma mark - 重新申请,该代码暂时未使用
-(void)renewApply{
    
    //删除原来的文件,产生新的文件
    NSFileManager *fm =  [NSFileManager defaultManager];
    
    //获取需要删除文件的路径
    
    NSString *file = [kAppplyModelFile jq_appendDocumentDir];
    
    NSError *error;
    [fm removeItemAtPath:file error:&error];
    
    if (!error) {
        
        NSLog(@"删除成功");
    }else{
        
        NSLog(@"删除失败");
    }
    
}


#pragma
#pragma mark - 搭建UI界面
-(void)setupUI{
    
    self.title = @"申请就医";
    
    self.tableView.bounces = NO;
#pragma mark - 获取医生数量
    
    //设置表位
    
    WGDiseaseFooterView *footerView = [WGDiseaseFooterView diseasefooterView];
    
    self.footerView = footerView;
    //设置申请按钮的属性
    self.btn_apply =  self.footerView.btn_apply;
    self.btn_apply.enabled = NO;
    
    
    self.tableView.tableFooterView = footerView;
    
    __weak typeof(self) weakSelf = self;
    
    
#pragma
#pragma mark - 点击申请按钮
    footerView.ClickApppyToLookAtDocotor = ^{
        
#pragma mark - 产生一个阴影遮罩
        UIView *coverView = [[UIView alloc]init];
        weakSelf.coverView = coverView;
        coverView.backgroundColor = [UIColor blackColor];
        [[UIApplication sharedApplication].keyWindow addSubview:coverView];
        [coverView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(weakSelf.tableView);
            make.width.mas_equalTo(SCREEN_WIDTH);
            make.height.mas_equalTo(SCREEN_HEIGHT);
        }];
        
        coverView.alpha = 0;
        [UIView animateWithDuration:1  animations:^{
            
            [coverView layoutIfNeeded];
            
            coverView.alpha = 0.8;
            
            [UIApplication sharedApplication].keyWindow.transform = CGAffineTransformMakeScale(0.8, 0.8);
            
        } completion:nil];
        
        
#pragma mark - 产生一个提示框
        //2.产生一个提示框
        UIButton *btn = [UIButton buttonWithType:0];
        weakSelf.applySecButton = btn;
        [coverView addSubview:btn];
        btn.titleLabel.font = [UIFont fontWithName:@"AppleGothic" size:20];
        [btn setTitle:@"✅ 恭喜你申请成功" forState:UIWindowLevelNormal];
        [btn setTitleColor:[UIColor jq_colorWithHex:kThemeTextColor] forState:UIControlStateNormal];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(weakSelf.coverView);
            
        }];
        
        [UIView animateWithDuration:1 animations:^{
            
            [btn layoutIfNeeded];
            
        } completion:^(BOOL finished)
         {
             
             [btn addTarget:weakSelf  action:@selector(clickRemoveCover) forControlEvents:UIControlEventTouchUpInside];
             
         }];
        
#pragma - 保存用户数据
        
        [self archiveApplyModel];
        
    };
    
}

#pragma
#pragma mark - 点击申请进行数据归档
-(void)archiveApplyModel{
    
    
    WGApplyModel *applyModel = [[WGApplyModel alloc]init];
    
    //疾病类型
    applyModel.diseaseType = self.sectionHeader;//种类
    applyModel.detailDisease = self.lb_diseaseName.text;//疾病明细
    applyModel.complicationDisease = self.lb_detailDiseases.text;//并发症
    applyModel.complicationCount = self.lb_detailCount.text;//并发症个数
    applyModel.docotorCount = self.doctorCount; //医生数量
    applyModel.quezhen = self.lb_IsQueZhen.text;//是否确诊
    applyModel.jieShou = self.lb_isJieShou.text;//是否接受
    applyModel.treatType = self.treatTypeButton.currentTitle;//获取治疗类型
    
    //获取归档路径
    
    NSString *docPath =  [kAppplyModelFile jq_appendDocumentDir];
    
    //归档对象
    //归档
    
    [NSKeyedArchiver archiveRootObject:applyModel toFile:docPath];
    
    NSLog(@"docPath = %@",docPath);
};


#pragma
#pragma mark - 移除动画
-(void)clickRemoveCover{
    
    
    [UIView animateWithDuration: 1 animations:^{
        
        self.coverView.alpha = 0;
        [UIApplication sharedApplication].keyWindow.transform = CGAffineTransformIdentity;
        
    } completion:^(BOOL finished) {
        
        [self.coverView removeFromSuperview];
        [self.applySecButton removeFromSuperview];
        
        [self.navigationController popToRootViewControllerAnimated:YES];
        
    }];
    
}





#pragma
#pragma mark - 提示框懒加载
-(WGDiseaseAleartView *)aleartView{
    
    if (!_aleartView) {
        
        _aleartView = [WGDiseaseAleartView aleartView];
        _aleartView.alpha = 0;
        
        [self.tableView addSubview: _aleartView];
        
        [self.tableView bringSubviewToFront: _aleartView];
        
        [ _aleartView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            //约束很重要
            make.centerX.mas_equalTo(self.tableView);
            make.bottom.mas_equalTo(self.footerView.lb_docoterCount.mas_top).offset(-10);
            
            make.width.mas_equalTo(250);
            make.height.mas_equalTo(60);
            
        }];
        
    }
    
    return _aleartView;
}



-(void)setModel:(HMHomeFunctionModel *)model{
    
    _model = model;
    
    self.lb_diseaseName.text = model.name;
    
    [self.tableView reloadData];
    
}

#pragma
#pragma mark - 设置第一组的组头
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectZero];
        label.text = [NSString stringWithFormat:@"    疾病类型:%@疾病",self.model.name];
        label.textColor = [UIColor jq_colorWithHex:kThemeTextColor];
        [label sizeToFit];
        
        //设置属性文字
        NSMutableAttributedString *textColor = [[NSMutableAttributedString alloc]initWithString:label.text];
        
        NSRange rangel = [[textColor string] rangeOfString:[label.text substringFromIndex:7]];
        [textColor addAttribute:NSForegroundColorAttributeName value:[UIColor jq_colorWithHex:kThemeTextColor] range:rangel];
        
        //设置范围内的文字的样式 （一个label中，设置不同的字体）
        [textColor addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:17] range:rangel];
        
        [label setAttributedText:textColor];
        
        self.sectionHeader = label.text;
        
        return label;
    }else{
        return 0;
    }
}

#pragma
#pragma mark - 设置表尾部高度

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section{
    
    return 120;
}


#pragma
#pragma mark - 执行跳转
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    
    WGDetailDiseasesController *desVc = segue.destinationViewController;
    
    //床底model
    desVc.model = self.model;
    
}

#pragma
#pragma mark - 点击手势跳转
- (IBAction)clickTap:(UITapGestureRecognizer *)sender {
    
    [self clickComplicationButton:nil];
    
}


#pragma
#pragma mark - 点击按钮执行跳转
- (IBAction)clickComplicationButton:(UIButton *)sender {
    
    WGComplicationwController *complicationVc = [[WGComplicationwController alloc]init];
    
    __weak typeof(self) weakSelf = self;
    
    complicationVc.ClickOKButton = ^(NSString *selStr,NSInteger count){
        
        weakSelf.lb_detailDiseases.text = selStr;
        [weakSelf.lb_detailDiseases sizeToFit];
        weakSelf.lb_detailDiseases.textColor = [UIColor jq_colorWithHex:kThemeTextColor];
        weakSelf.lb_detailCount.text = [NSString stringWithFormat:@"(共%zd条)",count];
        
#warning - 数据传递过来的时候必须刷新数据,否则产生 BUG
        
        [self.tableView reloadData];
        
    };
    
    [self.navigationController pushViewController:complicationVc animated:YES];
    
    
}


#pragma
#pragma mark - 视图将要出现获取疾病详情通知
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    //接受点击疾病详情cell的通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(receiveDetailDeseaseCellClicked:) name:kClickDetailCellNotiCation object:nil];
}

#pragma
#pragma mark -  实现通知
-(void)receiveDetailDeseaseCellClicked:(NSNotification *)noti{
    
    self.lb_diseaseName.text = noti.object;
    self.lb_diseaseName.textColor = [UIColor jq_colorWithHex:kThemeTextColor];
    
    [self.tableView reloadData];
    
}


#pragma
#pragma mark -  设置按钮的选中状态
- (IBAction)clickQueZhenButton:(UIButton *)sender {
    
    //设置按钮选中
    [self selectedWithButton:sender otherButton:self.btn_yiSi];
    
    [self setTextColor];
}

- (IBAction)clickSimilarDiseaseButton:(UIButton *)sender {
    
    [self selectedWithButton:sender otherButton:self.btn_queZhen];
    
    [self setTextColor];
    
}

//选择接受
- (IBAction)clickReceiveButton:(UIButton *)sender {
    
    [self selectedWithButton:sender otherButton:self.btn_noJIeShou];
    
    [self setTextColor];
    
    [self setAleartView];
    
}

- (IBAction)clickRefuseButton:(UIButton *)sender {
    
    [self selectedWithButton:sender otherButton:self.btn_jieShou];
    
    [self setTextColor];
    
    [self setAleartView];
    
}

#pragma
#pragma mark - 设置提示框
-(void)setAleartView{
    
    
    [UIView animateKeyframesWithDuration:0.8 delay:0.5 options:   UIViewKeyframeAnimationOptionLayoutSubviews   animations:^{
        
        self.aleartView.alpha = 1;
        
        [self.aleartView layoutIfNeeded];
        
    } completion: nil];
    
    
    //选择治疗类型
    __weak typeof(self) weakSelf = self;
    
    
#pragma mark - 选择治疗类型
    self.aleartView.SelectedTreatTypeBlock = ^(UIButton *button){
        
         weakSelf.treatTypeButton = button;
        
#pragma mark - 获取医生数量
        //显示医生的数量
        [[NetWorkManager sharedManager] loadDoctorCountWithC1_id:1 ci2_id:3 ci3_id:3 completed:^(int count) {
            
            if (count) {
                self.doctorCount = count;
                
                NSLog(@"self.doctorCount:%zd",self.doctorCount);
                
                [self.tableView reloadData];
                
                
                //表尾视图显示
                weakSelf.footerView.lb_docoterCount.hidden = NO;
                
                //设置医生匹配的数量
                weakSelf.footerView.lb_docoterCount.text = [NSString stringWithFormat:@"正在为你匹配到%zd名医生",self.doctorCount] ;
                ;
                
                //设置数字的属性文字属性文字
                NSMutableAttributedString *textColor = [[NSMutableAttributedString alloc]initWithString:weakSelf.footerView.lb_docoterCount.text];
                
                
                //获取字符串中的数字的
                NSRange range = [weakSelf.footerView.lb_docoterCount.text  rangeOfString:@(self.doctorCount).description];
                
                //设置指定范围的文字颜色
                [textColor addAttribute:NSForegroundColorAttributeName value:[UIColor jq_colorWithHex:kThemeTextColor] range:range ];
                
                //设置指定范围文字的大小
                [textColor addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:30] range:range ];
                
                [weakSelf.footerView.lb_docoterCount setAttributedText:textColor];
  
            }
            
        }];
        
#pragma mark - 修改申请按钮状态
       
        weakSelf.btn_apply.enabled = YES;
        
        [weakSelf.btn_apply setBackgroundColor:[UIColor jq_colorWithHex:kThemeTextColor]];
        
        [self.tableView reloadData];
        
    };
    
}


#pragma
#pragma mark - 判断按钮是否选中
-(void)selectedWithButton:(UIButton *)currentButton otherButton:(UIButton *)otherButton{
    
    if (currentButton.selected == NO) {
        currentButton.selected = YES;
        otherButton.selected = NO;
        
    }else{
        
        otherButton.selected = NO;
        currentButton.selected = YES;
    }
}

#pragma
#pragma mark - 设置申请就医按钮的实现
-(void)setApplyButtonBackgroudColor{
    if (self.btn_jieShou.isSelected || self.btn_queZhen.isSelected || self.btn_noJIeShou || self.btn_yiSi.isSelected) {
        
        [self.btn_apply setBackgroundColor:[UIColor jq_colorWithHex:0X00BAB6]];
    }else{
        [self.btn_apply setBackgroundColor:[UIColor lightGrayColor]];
    }
    
}

#pragma
#pragma mark -  设置是否却确诊和是否接受治疗的文字颜色及文字发生变化

-(void)setTextColor{
    
    if (self.btn_jieShou.isSelected || self.btn_noJIeShou.isSelected) {
        self.lb_isJieShou.textColor = [UIColor jq_colorWithHex:kThemeTextColor];
        
        if (self.btn_jieShou.isSelected) {
            
            self.lb_isJieShou.text = self.btn_jieShou.currentTitle;
            
        }
        if (self.btn_noJIeShou.isSelected) {
            self.lb_isJieShou.text = self.btn_noJIeShou.currentTitle;
            
        }
        
    }
    
    if (self.btn_yiSi.isSelected || self.btn_queZhen.isSelected) {
        
        self.lb_IsQueZhen.textColor = [UIColor jq_colorWithHex:kThemeTextColor];
        
        if (self.btn_yiSi.isSelected) {
            
            self.lb_IsQueZhen.text = self.btn_yiSi.currentTitle;
        }
        
        if (self.btn_queZhen.isSelected) {
            
            self.lb_IsQueZhen.text = self.btn_queZhen.currentTitle;
        }
    }
    
}



#pragma
#pragma mark - dealloc

-(void)dealloc{
    
    NSLog(@"%@销毁了",[self class]);
    
}

@end
